'''
Company: TWL
Author: xue jian
Email: xuejian@kanzhun.com
Date: 2020-09-04 09:06:28
'''

'''
257. 二叉树的所有路径
给定一个二叉树，返回所有从根节点到叶子节点的路径。

说明: 叶子节点是指没有子节点的节点。

示例:

输入:

   1
 /   \
2     3
 \
  5

输出: ["1->2->5", "1->3"]

解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3

tops:简单递归解决。注意两点，要再页节点的时候插入返回值，这个时候需要特殊处理一下空list。代码如下
'''

from tree_node import *
from typing import *

class Solution:
    def binaryTreePaths(self, root: TreeNode) -> List[str]:
        re = []
        def recurse(root, s):
            s += '->'+str(root.val)
            if not root.left and not root.right:
                re.append(s[2:])
            if root.left:
                recurse(root.left, s)
            if root.right:
                recurse(root.right, s)
            s = s[:-2]
        if not root:
            return re
        s = ''
        recurse(root, s)
        return re

if __name__ == "__main__":
    solution = Solution()
    null = 'null'
    root = [7,1,4,6,null,5,3,null,null,null,null,null,2]
    for i,v in enumerate(root):
        root[i] = str(v)
    s = ','.join(root)
    # print(s)
    o_s = OfficialSerialize()
    root = o_s.deserialize(s)
    print(solution.binaryTreePaths(root))